Setup and storing functions

knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(here)
library(tidyverse)
library(janitor)
library(broom)
library(equatiomatic)

options(scipen = 999)


electricity <- read_csv(here("HW3_data.csv")) %>% 
  clean_names() %>% 
  select(-1)

# running linear models to get demand curves for high and low income consumers
model_demand_l <- lm(price_cents  ~ q_low_kwh, data = electricity)
model_demand_h <- lm(price_cents ~ q_high_kwh, data = electricity)


## FUNCTIONS

# demand 
demand <- function(p, model){
  q <- (p - model$coefficients[[1]])/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand
demand_agg <- function(p){
  q <- demand(p, model_demand_l) + demand(p, model_demand_h)
  return(q)
}

# consumer surplus
CS <- function(p, model){
  q <- demand(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# aggregate consumer surplus
CS_agg <- function(p){
  cs <- CS(p,model_demand_l) + CS(p,model_demand_h)
  return(cs)
}

# producer surplus function
PS <- function(p){
  ps <- (p*demand_agg(p))/2 # producer surplus
  return(ps)
}

# marginal cost functions
mc_slope <- 10/demand_agg(10)
# marginal cost function
mc <- function(q){
  p <- q*(mc_slope)
  return(p)
}

mc_q <- function(p){
  q <- p/mc_slope
  return(q)
}

# social cost of carbon to mec function
scc <- function(scc){
  mec <- (0.85/metric_ton)*100*scc
  return(mec)
}
# determining marginal external cost
metric_ton <- 2204.62
mec_cents <- (0.85/metric_ton)*100*51

Visualization

FIGURE THIS OUT

1. Price per kwh of electricity

metric_ton <- 2204.62
price_per_kwh_cents<- (0.85/metric_ton)*100*51

Marginal external cost per kWh of electricity: 1.97 cents


2. Supply and demand curves, consumer/producer benefits

# storing slopes and intercepts of each equation to report inline code 
int_high <- model_demand_h$coefficients[1]
slope_high <- model_demand_h$coefficients[2]
int_low <- model_demand_l$coefficients[1]
slope_low <- model_demand_l$coefficients[2]

# calculating surpluses and environmental costs
cs2 <- CS_agg(10) # consumer surplus
ps2 <- 10*demand_agg(10)/2 # producer surplus
enviro_cost_3 <- mec_cents*demand_agg(10) # environmental cost

Demand curve for high income consumers: P = 31.61 -0.000052Q

Demand curve for low income consumers: P = 23.37 -0.00011Q

Aggregate demand curve: To find the aggregate demand curve, horizontally sum the two demand curves above. FIND ANSWER TO THIS

Supply curve: P = 0.000019Q

Consumer benefit: $52987.22

Producer benefit: $26835.97

Environmental Cost: $10553.65


3. Consumer benefit by population

# finding the consumer surplus for each income group
cs_low3 <- CS(10, model_demand_l) # consum
cs_high3 <- CS(10, model = model_demand_h)

Consumer benefit for high income consumers: $44874.79

Consumer benefit for low income consumers: $8112.43


4. Optimal electricity tax

# finding the new demand curve under the tax 
demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p){
  q <- demand_after_tax(p, model_demand_l, mec = mec_cents) + demand_after_tax(p, model_demand_h, mec = mec_cents)
  return(q)
}
# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p) - mc_q(p),
        interval = c(0,20))

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

price_after_tax<- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- mec_cents*demand_agg_after_tax(price_after_tax)
  
tax_revenue <- mec_cents*demand_agg_after_tax(price_after_tax)

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax)

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p){
  ps <- (p*demand_agg_after_tax(p))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax)

Optimal Electricity Tax: 1.97 cents

A. The amount of electricity produced and consumed: 500284.52kWh

B. The price of electricity: 9.32 cents per kWh before the tax is applied

C. Overall welfare of “high” income consumers: $39686.25

D. Overall welfare of “low” income consumers: $-3211.85, meaning there is a net loss of welfare to low income consumers

E. Wlefare to power suppliers: $23316.147

F. Total environmental damage: $9837.22

G. Total tax revenue generated: $9837.22


5. Tax redistribution

# calculating relative electricity use under the status quo 
proportion_high <- demand(model_demand_h, p = 10)/((demand(model_demand_h, p = 10)) + demand(model_demand_l, p = 10))
proportion_low <- demand(model_demand_l, p = 10)/((demand(model_demand_h, p = 10)) + demand(model_demand_l, p = 10))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

SCC = $51

Overall welfare of “high” income consumers: $47299.43

Overall welfare of “low” income consumers: $-987.81, meaning there is a net loss of welfare to low income consumers

Welfare to power suppliers: $233.161


SCC = $75

scc5 <- 75

demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p, mec){
  q <- demand_after_tax(p, model_demand_l, mec = mec) + demand_after_tax(p, model_demand_h, mec = mec)
  return(q)
}

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p, mec = scc(scc5)) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- mec_cents*demand_agg_after_tax(price_after_tax, mec = scc(scc5))
  
tax_revenue <- mec_cents*demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p, mec){
  ps <- (p*demand_agg_after_tax(p, mec = mec))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax, mec = scc(scc5))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

Overall welfare of “high” income consumers: $48296.19

Overall welfare of “low” income consumers: $-371.94, meaning there is a net loss of welfare to low income consumers

Welfare to power suppliers: $21745.342


SCC = $100

# inputting social cost of carbon
scc5 <- 100

# finding the new demand curve under the tax 
demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p, mec){
  q <- demand_after_tax(p, model_demand_l, mec = mec) + demand_after_tax(p, model_demand_h, mec = mec)
  return(q)
}

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p, mec = scc(scc5)) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- mec_cents*demand_agg_after_tax(price_after_tax, mec = scc(scc5))
  
tax_revenue <- mec_cents*demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p, mec){
  ps <- (p*demand_agg_after_tax(p, mec = mec))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax, mec = scc(scc5))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

Overall welfare of “high” income consumers: $49355.35

Overall welfare of “low” income consumers: $279.43

Welfare to power suppliers: $20167.331


SCC = $125

# inputting social cost of carbon
scc5 <- 125

# finding the new demand curve under the tax 
demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p, mec){
  q <- demand_after_tax(p, model_demand_l, mec = mec) + demand_after_tax(p, model_demand_h, mec = mec)
  return(q)
}

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p, mec = scc(scc5)) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- mec_cents*demand_agg_after_tax(price_after_tax, mec = scc(scc5))
  
tax_revenue <- mec_cents*demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p, mec){
  ps <- (p*demand_agg_after_tax(p, mec = mec))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax, mec = scc(scc5))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

Overall welfare of “high” income consumers: $50435.8

Overall welfare of “low” income consumers: $940.86

Welfare to power suppliers: $18648.753


SCC = $150

# inputting social cost of carbon
scc5 <- 150

# finding the new demand curve under the tax 
demand_after_tax <- function(p, model, mec){
  q <- (p - model$coefficients[[1]] + mec)/model$coefficients[[2]]
  q <- ifelse(q<0,0,q)
  return(q)
}

# aggregate demand curve function after tax
demand_agg_after_tax <- function(p, mec){
  q <- demand_after_tax(p, model_demand_l, mec = mec) + demand_after_tax(p, model_demand_h, mec = mec)
  return(q)
}

# new consumer surplus function
CS_after_tax <- function(p, model){
  q <- demand_after_tax(p, model)
  cs <- 0.5*(model$coefficients[[1]] - p)*q
  return(cs)
}

# setting the functions equal to determine new equilibrium price after tax
uniroot_after_tax <- uniroot(function(p)
  demand_agg_after_tax(p, mec = scc(scc5)) - mc_q(p),
        interval = c(0,20))

price_after_tax <- uniroot_after_tax$root

# total environmental damage
enviro_damage_after_tax <- mec_cents*demand_agg_after_tax(price_after_tax, mec = scc(scc5))
  
tax_revenue <- mec_cents*demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# amount of electricity consumed after tax
consumption_after_tax <- demand_agg_after_tax(price_after_tax, mec = scc(scc5))

# overall welfare to high and low income consumers
cs_high_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_h)
cs_low_after_tax <- CS(p = price_after_tax + mec_cents, model = model_demand_l) - enviro_damage_after_tax

# welfare for electricity producer
PS_after_tax <- function(p, mec){
  ps <- (p*demand_agg_after_tax(p, mec = mec))/2 # producer surplus
  return(ps)
}
ps_after_tax <- PS_after_tax(price_after_tax, mec = scc(scc5))

welfare_h_5 <- (cs_high_after_tax + proportion_high*tax_revenue)/100 
welfare_l_5 <- (cs_low_after_tax + proportion_low*tax_revenue)/100 
ps_5 <- ps_after_tax/100

Overall welfare of “high” income consumers: $51537.53

Overall welfare of “low” income consumers: $1612.33

Welfare to power suppliers: $17189.609

6. Solar generation

  1. What is total electricity consumption?
  2. What is the total environmental externality?
  3. What value of the electricity tax makes the total environmental damage the same as the damage when solar panels are available to the high income group?